package com.ikas.ai.server.module.morningpaper.model.elements.condition;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import com.ikas.ai.server.module.data.model.BaseMete;
import com.ikas.ai.server.module.data.model.tdengine.RealDataHis;
import com.ikas.ai.server.module.morningpaper.model.elements.AbstractElementNeedsTDEngine;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.Instant;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
public abstract class AbstractElementMachineConditionCost extends AbstractElementNeedsTDEngine {

    protected static final BigDecimal DIVIDE_FACTOR = new BigDecimal(1000 * 60 * 60);

    protected Double fillConditionHours(AbstractElementMachineConditionCost element, List<RealDataHis> meteHis) {
        if (CollectionUtils.isEmpty(meteHis)) {
            return 0.0;
        }
        BaseMete baseMete = getBaseMeteYxCache(getMeteId(element));
        Integer validMeteValue = baseMete.getValidMeteValue();
        meteHis = meteHis.stream().sorted(Comparator.comparingLong(r -> r.getIdTime().getTime())).collect(Collectors.toList());
        int inx = -1;
        for (int i = 0; i < meteHis.size(); i++) {
            if (String.valueOf(validMeteValue).equals(meteHis.get(i).getValue())) {
                inx = i;
                break;
            }
        }
        if (inx != -1) {
            meteHis = meteHis.subList(inx, meteHis.size());
        }
        long sumTime = 0L;
        List<RealDataHis> validMeteHis = meteHis.stream().filter(f -> validMeteValue.toString().equals(f.getValue())).collect(Collectors.toList());
        List<RealDataHis> invalidMeteHis = meteHis.stream().filter(f -> !validMeteValue.toString().equals(f.getValue())).collect(Collectors.toList());
        for (int i = 0; i < validMeteHis.size(); i++) {
            if (validMeteValue.equals(Integer.valueOf(validMeteHis.get(i).getValue()))) {
                Instant startTimeFlag = validMeteHis.get(i).getIdTime().toInstant();
                if (i < invalidMeteHis.size() && Objects.nonNull(invalidMeteHis.get(i))) {
                    Instant endTimeFlag = invalidMeteHis.get(i).getIdTime().toInstant();
                    long millis = Duration.between(startTimeFlag, endTimeFlag).toMillis();
                    sumTime += millis;
                } else {
                    DateTime now = DateTime.now();
                    now.setField(DateField.HOUR_OF_DAY, 8);
                    now.setField(DateField.MINUTE, 0);
                    now.setField(DateField.SECOND, 0);
                    now.setField(DateField.MILLISECOND, 0);
                    long millis = Duration.between(startTimeFlag, now.toInstant()).toMillis();
                    sumTime += millis;
                }
            }
        }
        BigDecimal hoursBigDecimal = new BigDecimal(sumTime).divide(DIVIDE_FACTOR, 2, RoundingMode.HALF_UP);
        return Math.abs(hoursBigDecimal.doubleValue());
    }
}
